<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>景点管理界面</title>
    <link rel="icon" href="../assets/images/favicon.ico"/>
    <link rel="stylesheet" href="../assets/libs/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/module/formSelects/formSelects-v4.css" media="all">
    <link rel="stylesheet" href="../assets/common.css" media="all">
</head>

<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body layui-form">
            <div style="display: inline-block;width: 100px;margin-right: 10px;">
                <select id="sltKey">
                    <option value="">搜索条件</option>
                    <option value="name">景点名称</option>
                    <option value="place">目的地</option>
                    <option value="perice">价格</option>
                    <option value="type">旅游类型</option>
                </select>
            </div>
            <div style="display: inline-block;margin-right: 10px;width: 160px;">
                <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
            </div>
            <div class="layui-inline">
                <button id="scenicBtnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                <button id="scenicBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            </div>

            <table class="layui-table" id="scenicTable" lay-filter="scenicTable"></table>
        </div>
    </div>

</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tbaleState">
    <input type="checkbox" lay-filter="ckStatu" lay-skin="switch" lay-text="正常|锁定"
           value="{{d.id}}" {{d.statu==0?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="scenicForm">
    <form lay-filter="scenicForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">景点名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入景点名称" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">目的地</label>
            <div class="layui-input-block">
                <input name="place" placeholder="请输入目的地" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-block">
                <input name="price" placeholder="请输入价格" type="text" class="layui-input"
                       lay-verify="required|number" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">旅游类型</label>
            <div class="layui-input-block">
                <select name="type"  lay-verify="required">
                    <option value="">请选择旅游类型</option>
                    <option value="recommend">热门推荐</option>
                    <option value="domestic">国内旅游</option>
                    <option value="abroad">境外游</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">旅游路线</label>
            <div class="layui-input-block">
                <input name="route" placeholder="请输入旅游路线" type="text" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">景点介绍</label>
            <div class="layui-input-block">
                <textarea name="introduce" placeholder="请输入景点介绍" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="scenicFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../assets/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'formSelects', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#scenicTable',
            url: base_server + 'scenic/query',
            method: 'get',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'name', title: '景点名称'},
                {field: 'price', title: '价格/元'},
                {
                    templet: function (d) {
                       var type=d.type;
                       if(type=="recommend"){
                           return "热门推荐";
                       }else if(type=="domestic"){
                           return "国内旅游";
                       }else{
                           return "境外游";
                       }
                    }, title: '旅游类型'
                },
                {field: 'place', title: '目的地'},
                {
                    templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {field: 'statu', templet: '#tbaleState', title: '状态', unresize: true, width: 100},
                {align: 'center', toolbar: '#tableBar', title: '操作', unresize: true, width: 200}
            ]],
            skin: 'line',
            size: 'lg'
        });

        // 搜索按钮点击事件
        $('#scenicBtnSearch').click(function () {
            var key = $('#sltKey').val();
            var value = $('#edtSearch').val();
            table.reload('scenicTable', {where: {searchKey: key, searchValue: value}});
        });

        // 添加按钮点击事件
        $('#scenicBtnAdd').click(function () {
            showAddModel();
        });

        // 工具条点击事件
        table.on('tool(scenicTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此景点吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('scenic/delete/' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }, 'DELETE');
                });
            } else if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }
        });

        // 修改scenic状态
        form.on('switch(ckStatu)', function (obj) {
            layer.load(2);
            admin.req('scenic/statu', {
                id: obj.elem.value,
                statu: obj.elem.checked ? 0 : 1
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'PUT');
        });

        // 显示表单弹窗
        function showEditModel(data) {
            console.log(data);
            layer.open({
                type: 1,
                area: '400px',
                offset: '65px',
                title: '修改景点',
                content: $('#scenicForm').html(),
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    form.render();
                    // 回显scenic数据
                    if (data) {
                        form.val('scenicForm', data);
                    }
                    // 表单提交事件
                    form.on('submit(scenicFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req('scenic/update', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                layer.closeAll('page');
                                table.reload('scenicTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },'PUT');
                        return false;
                    });
                }
            });
        }

        function showAddModel() {
            layer.open({
                type: 1,
                area: '400px',
                offset: '65px',
                title: '添加景点',
                content: $('#scenicForm').html(),
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    form.render();

                    // 表单提交事件
                    form.on('submit(scenicFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req('scenic/insert', d.field, function (res) {
                            console.log(d);
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                layer.closeAll('page');
                                table.reload('scenicTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                        return false;
                    });
                }
            });
        }


    });
</script>

</body>
</html>